﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKRevitBase.WinForm;
using YJKRevitView;

namespace YJKRevitBeam
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdSlopeBeamCreate : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)123;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_cmdData = cmdData;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_selection = cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.m_selection);
			if (CmdSlopeBeamCreate._hWndRevit == null)
			{
                CmdSlopeBeamCreate._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			SlopeBeamCreateForm slopeBeamCreateForm = new SlopeBeamCreateForm(cmdData);
			slopeBeamCreateForm.Show(CmdSlopeBeamCreate._hWndRevit);
			int wMsg = 7;
			CmdSlopeBeamCreate.SendMessage(CmdSlopeBeamCreate._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			Transaction transaction = new Transaction(this.m_doc, "生成斜梁");
			Result result;
			try
			{
				for (;;)
				{
					transaction.Start();
					BeamFilter beamFilter = new BeamFilter();
					Reference reference = this.m_selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, beamFilter, "请选择需要偏移的梁端");
					XYZ globalPoint = reference.GlobalPoint;
					FamilyInstance familyInstance = this.m_doc.GetElement(reference) as FamilyInstance;
					if (familyInstance != null)
					{
						Curve curve = (familyInstance.Location as LocationCurve).Curve;
						XYZ endPoint = curve.GetEndPoint(0);
						XYZ endPoint2 = curve.GetEndPoint(1);
						double num = 0.0;
						try
						{
							num = double.Parse(slopeBeamCreateForm.EndOffset);
							num = AssistFunc.mmToFeet(num);
						}
						catch
						{
							slopeBeamCreateForm.EndOffset = slopeBeamCreateForm.m_offset;
							transaction.RollBack();
							TaskDialog.Show("盈建科", "请输入合法字符。");
							continue;
						}
						if (globalPoint.DistanceTo(endPoint) < globalPoint.DistanceTo(endPoint2))
						{
							Parameter parameter = familyInstance.get_Parameter(BuiltInParameter.STRUCTURAL_BEAM_END0_ELEVATION);
							double num2 = parameter.AsDouble();
							parameter.Set(num2 + num);
						}
						else
						{
							Parameter parameter2 = familyInstance.get_Parameter(BuiltInParameter.STRUCTURAL_BEAM_END1_ELEVATION);
							double num3 = parameter2.AsDouble();
							parameter2.Set(num3 + num);
						}
					}
					transaction.Commit();
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				slopeBeamCreateForm.Close();
				transaction.RollBack();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception)
			{
				transaction.RollBack();
				slopeBeamCreateForm.Close();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		private ExternalCommandData m_cmdData;

		private Document m_doc;

		private Selection m_selection;

        public static WindowHandle _hWndRevit;
	}
}
